

    let http = require('http');
    let fs = require('fs');
    let querystring = require('querystring');
    const users = new Map([["tom", "123456"], ["jack", "654321"]]);

    let server = http.createServer();

    server.on('request', function(req, res) {
        let url = req.url;
        if(url == '/') {
            res.writeHead(200, {'content-type': 'text/html'});
            fs.readFile('index.html', 'utf-8', function(err, data) {
                if(err) return res.end('404 not found.');
                return res.end(data);
            });
        }
        else if(url == '/login') {
            res.writeHead(200, {'content-type': 'text/html'});
            fs.readFile('login.html', 'utf-8', function(err, data) {
                if(err) return res.end('404 not found.');
                return res.end(data);
            });
        }
        else if(url == '/checklogin') {
            let body = '';
            // 接收数据
            req.on('data', function(data) {
                body += data;
            });
            req.on('end', function() {
                let userdata = querystring.parse(body);
                console.log("userame:"+userdata.username + "    password:" + userdata.password);
                let userps = users.get(userdata.username);
                let content = fs.readFileSync("./loginStatus.html");
                if(users && (userps == userdata.password)) {
                    console.log("验证通过");
                    content = `${userdata.username}已登录`;
                    res.writeHead('200', {'content-type': 'text/html; charset="utf8"'});
                    res.write(content);
                    // content = content.toString().replace("{{loginStatus}}", userdata.username+"已登录");
                }
                else {
                    console.log("账号密码错误");
                    content = '账号密码错误';
                    res.writeHead('200', {'content-type': 'text/html; charset="utf8"'});
                    res.write(content);
                    // content = content.toString().replace("{{loginStatus}}", "账号密码错误");
                }
                res.end();
            });
        }
        else {
            res.writeHead(404, {'content-type': 'text/html'});
            res.end('404 not found.');
        }
    });

    server.listen(3000, function() {
        console.log("Server is runing in Port 3000...");
    });